<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use Doctrine\DBAL\Schema\Table;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\OSS;
use Illuminate\Support\Facades\DB;

class CauseController extends ApiController
{

    private $uid = '';


    public function __construct()
    {
        $this->uid = auth('api')->id();
    }

    //请假
    public function setCourse(Request $request){
        $Course = $request['Course'];
        $start_date = strtotime($request['start_date']);
        $end_date = strtotime($request['end_date']);
        //查看当前人省份
        $user_status = User::where('id',$this->uid)->first(['level','class_end_time']);
        if ($user_status->level == 0){
            return parent::ajaxReturn(201, new \stdClass(), '您还不是学员');
        }
        //查看当前人是否再有效期内
        if ($user_status->class_end_time < time()){
            return parent::ajaxReturn(201, new \stdClass(), '您的课时卡已经过期');
        }

        $res = DB::table('cause')->insert([
            'user_id'=>$this->uid,
            'causation'=>$Course,
            'start_date'=>$start_date,
            'end_date'=>$end_date,
        ]);
        if ($res){
            return self::returnAjax('', 200, '提交成功');
        }else{
            return parent::ajaxReturn(201, new \stdClass(), '提交失败');
        }
    }

    //个人修课
    public function levCourse(Request $request){
        $pic = $request['CourPic'];
        $Course = $request['Course'];
        $user_status = User::where('id',$this->uid)->first(['level','class_end_time']);
        if ($user_status->level == 0){
            return parent::ajaxReturn(201, new \stdClass(), '您还不是学员');
        }
        //查看当前人是否再有效期内
        if ($user_status->class_end_time < time()){
            return parent::ajaxReturn(201, new \stdClass(), '您的课时卡已经过期');
        }


        $res = DB::table('lev_course')->insert([
            'user_id'=>$this->uid,
            'causation'=>$Course,
            'img'=>$pic,
            'created_at'=>time(),
        ]);
        if ($res){
            return self::returnAjax('', 200, '提交成功');
        }else{
            return parent::ajaxReturn(201, new \stdClass(), '提交失败');
        }
    }


}
